<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\crawler\controller; //Demo插件英文名，改成你的插件英文就行了

use cmf\controller\PluginAdminBaseController;
use think\Db;

use plugins\crawler\model\CrawlerSiteModel;
class AdminIndexController extends PluginAdminBaseController
{

    protected function _initialize()
    {
        parent::_initialize();
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        }
    }

    /**
     * 采集插件
     * @adminMenu(
     *     'name'   => '采集插件',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '采集分享大师数据',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $sites = CrawlerSiteModel::all();

        //$sites = Db::name("crawler_site")->all();
        //$demos = PluginDemoModel::all();

        // print_r($demos);

        $this->assign("sites", $sites);

        return $this->fetch('/admin_index');
    }

    /**
     * 添加站点
     * @adminMenu(
     *     'name'   => '添加站点',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加站点',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        return $this->fetch('/admin_add');
    }

    /**
     * 保存站点
     * @adminMenu(
     *     'name'   => '保存站点',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '保存站点',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
            
            $result = $this->validate($this->request->param(), 'CrawlerSite');
            
            $data['sitename']        = $this->request->param('sitename');
            $data['siteurl']         = $this->request->param('siteurl');
            $data['charset']         = $this->request->param('charset');
            $data['create_time']     = time();

            if ($result !== true) {
                // 验证失败 输出错误信息
                $this->error($result);
            } else {
                $result = DB::name('crawler_site')->insert($data);
                if ($result !== false) {
                    $this->success("保存成功！");
                } else {
                    $this->error("保存失败！");
                }
            }
        }
    }
     
    /**
     * 删除站点
     * @adminMenu(
     *     'name'   => '删除采集站点',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除采集规则 如规则下面有列表则无法删除',
     *     'param'  => ''
     * )
     */
     public function delete()
     {
         $id = $this->request->param("id", 0, 'intval');
 
         if (empty($id)) {
             $this->error(lang("NO_ID"));
         }
         $crawlerSiteModel = new CrawlerSiteModel();
 
         $siteCount = Db::name('crawler_rule')->where('crawler_site_id', $id)->count();
         
         if($siteCount) {
             $this->error(lang("请先删除采集规则"));
         }
         else
         {
             $crawlerSiteModel->where(['id' => $id])->delete();
             $this->success(lang("DELETE_SUCCESS"));
         }
     }

    /**
     * 演示插件设置
     * @adminMenu(
     *     'name'   => '演示插件设置',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '演示插件设置',
     *     'param'  => ''
     * )
     */
    public function setting()
    {
        $users = Db::name("user")->limit(0, 5)->select();
        //$demos = PluginDemoModel::all();

        // print_r($demos);

        $this->assign("users", $users);


        $this->assign("users", $users);

        return $this->fetch('/admin_index');
    }

}
